<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Tree UI</title>
    <style>
      .tree-node {
        padding: 5px 0;
      }

      .tree-node.collapsed > .children {
        display: none;
      }

      .tree-node-toggle {
        cursor: pointer;
      }
    </style>
  </head>

  <body>
    <div id="tree-root">
      <div class="tree-node">
        <span class="tree-node-toggle">+</span> Root Node
        <div class="children">
          <div class="tree-node">
            <span class="tree-node-toggle">+</span> Child Node 1
            <div class="children">
              <div class="tree-node">
                <span class="tree-node-toggle">+</span> Grandchild Node 1
                <div class="children"></div>
              </div>
              <div class="tree-node">
                <span class="tree-node-toggle">+</span> Grandchild Node 2
                <div class="children"></div>
              </div>
            </div>
          </div>
          <div class="tree-node">
            <span class="tree-node-toggle">+</span> Child Node 2
            <div class="children"></div>
          </div>
        </div>
      </div>
    </div>

    <script>
      const nodes = document.querySelectorAll(".tree-node-toggle");

      nodes.forEach((node) => {
        node.addEventListener("click", () => {
          const parentNode = node.parentNode;
          parentNode.classList.toggle("collapsed");
          const isCollapsed = parentNode.classList.contains("collapsed");
          node.textContent = isCollapsed ? "+" : "-";
        });
      });
    </script>
  </body>
</html>
